<?php
/*
 * @Author: 逅来
 * @Description: 
 * @Date: 2023-03-20 09:29:05
 * @LastEditTime: 2023-03-29 10:32:44
 * @FilePath: \zhaopian.com\application\api\controller\photo\Itinerary.php
 * @email: 934749708@qq.com
 */
/*
 * @Author: 逅来
 * @Description: 
 * @Date: 2023-03-20 09:29:05
 * @LastEditTime: 2023-03-20 11:06:22
 * @FilePath: \xiangce_houtai-master\application\api\controller\photo\Itinerary.php
 * @email: 934749708@qq.com
 */
/*
 * @Author: 逅来
 * @Description: 
 * @Date: 2023-03-19 14:55:20
 * @LastEditTime: 2023-03-20 09:51:17
 * @FilePath: \xiangce_houtai-master\application\api\controller\photo\Itinerary.php
 * @email: 934749708@qq.com
 */



namespace app\api\controller\photo;

use app\common\controller\Api;
use app\admin\model\User as UserModel;
use app\admin\model\photo\City as CityModel;
use app\admin\model\photo\Route as RouteModel;
use app\admin\model\photo\Itinerary as ItineraryModel;
use app\admin\model\photo\Bind as BindModel;
use app\admin\model\photo\Related as RelatedModel;
use think\Request;
use app\api\controller\Validate;
use Think\Db;
class Itinerary extends Api
{
    // 无需登录的接口,*表示全部
    protected $noNeedLogin = ['*'];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['*'];
    public function _initialize()
    {
        // $data = $this->request->param('');
        // $user = UserModel::where('id', $data['user_id'])->where('token',$data['token'])->where('Expiration_time','> time',time())->find();
        // if(!$user){
        //     json(array('code' => 200, 'msg' => 'token过期重新登录', 'type' => 0))->send();
        //     exit();
        // }
        parent::_initialize();
         $this->Related = new \app\admin\model\photo\Related;
         
        $this->Bind = new \app\admin\model\photo\Bind;
        
    }
    public function succ_ess($number, $text, $title, $list, $data)
    {
        return json(['code' => $number, 'msg' => $text, $title => $list, 'data' => $data]);
    }
    // 行程图片详情
    public function ItineraryimgDelte()
    {
        $user_id = $this->auth->id;
        $data = $this->request->param('');

        $list = $this->Bind
            ->with(['user' => function ($query) {
                $query->field('id,nickname,avatar');
            }])
            ->where('itinerary_id', $data['itinerary_id'])
            // ->where('user_id', $data['user_id'])
            ->select();
        foreach ($list as $k => &$v) {

            $v['file'] = $this->Related
                ->with(['file' => function ($query) {
                    $query->field('id,fullurl');
                }])
                ->where('user_id', $v['user']['id'])
                ->where('itinerary_id', $data['itinerary_id'])
                ->order('createtime desc')
                ->field('file_id,createtime')
                ->select();
                if(empty($v['file'])){
                    $v['createtime'] = '';
                }else{
                    $v['createtime'] = date('Y-m-d H:i',$v['file'][0]['createtime']);
                }
                $v['length'] = count($v['file']);
              
              
               
        }

        return $this->succ_ess(200, '行程图片详情', 'type', 1, $list);
    }
    // 行程图片详情
    public function Itineraryimg(){
          $data = $this->request->param('');

        $city_id = Db::table('fa_photo_route')->where('id', Db::table('fa_photo_itinerary')->where('id', $data['itinerary_id'])->value('route_id'))->value('city_id');

        $city = Db::table('fa_photo_city')->whereIn('id', $city_id)->field('id,name')->select();
       
        foreach ($city as $k => &$v) {
            $v['file'] = $this->Related
                ->with(['file' => function ($query) {
                    $query->field('id,fullurl');
                }])
                ->where('user_id', $data['user_id'])
                ->where('city_id', $v['id'])
                ->field('file_id')
                ->select();
                $v['length'] = count($v['file']);
        }
        return $this->succ_ess(200, '行程图片详情', 'type', 1, $city);
    }
    // 行程图片详情
    public function Itineraryimg1(){
         $data = $this->request->param('');
        //   $list = $this->Related->with(['file','city'])->where('user_id',36)->where('itinerary_id',50)->where('city_id',9)->select();
          $list = $this->Related->with(['file','city','user'])->where('user_id',$data['yonghu_id'])->where('itinerary_id',$data['itinerary_id'])->select();
          foreach ($list as $k => &$v){
             $v['url'] = $v['file']['fullurl'];
          }
        //   die;
          if($list){
            return $this->succ_ess(200, '行程图片详情', 'type', 1, $list);
        }
      
    }
    /**
     * @description: 摄影师创建行程
     * @return {*}
     * @author: 逅来
     */
    public function add()
    {
        
        $data = $this->request->param('');
        $request = Request::instance();
        $domain = $request->domain();
        $Validate = new Validate();
        $Validate->add($data);
        $Itinerary = new ItineraryModel();
        $Itinerary->name = $data['name'];
        $Itinerary->route_id = $data['route_id'];
        $Itinerary->starttime = $data['starttime'];
        $Itinerary->overtime = $data['overtime'];
        $Itinerary->Trips = $data['Trips'];
        $Itinerary->number = $data['number'];
        $Itinerary->account_id = $data['user_id'];
        $Itinerary->save();
        $Itinerary = ItineraryModel::get($Itinerary->id);
        $Itinerary->qr = 'https://demo.dzqiji.com/qrcode/build?text=https://demo.dzqiji.com/xcx/'.$Itinerary->id;
        $res= $Itinerary->save();
        if($res){
            return $this->succ_ess(200, '创建行程成功', 'type', 1, '');
        }
    /**
     * @description: 行程详情
     * @return {*}
     * @author: 逅来
     */
    } 
    public function detailed(){
        $data = $this->request->param('');
        $Validate = new Validate();
        $Validate->detailed($data);
       
        $Itinerary = ItineraryModel::get($data['id']);
        $route =RouteModel::where('id',$Itinerary->route_id)->find();
        // 文件上传数量
        $Itinerary['file_count'] = RelatedModel::where('itinerary_id',$data['id'])->count();
        // 上传文件的人数
        $Itinerary['number_count'] =RelatedModel::where('itinerary_id',$data['id'])->group('user_id')->count();
        
        $related = Db::table('fa_photo_related')->alias('a')->join(' fa_user b', 'b.id = a.user_id','LEFT')->where('a.itinerary_id',$data['id'])->group('a.user_id')->field('a.itinerary_id,a.user_id,b.nickname,b.avatar')->select();
        foreach ($related as $k => &$v){
            $v['file'] = Db::table('fa_photo_related')
                            ->alias('a')
                            ->join('fa_photo_file b', 'b.id = a.file_id','LEFT')
                            ->where('a.itinerary_id',$data['id'])
                            ->where('a.user_id',$v['user_id'])
                            ->field('a.file_id,b.fullurl')
                            ->select();
        }
        $route = RouteModel::get($Itinerary->route_id);
        $Itinerary['city'] = Db::table('fa_photo_city')->whereIn('id', $route->city_id)->field('id,name')->select();
       $Itinerary['related']= $related;
       if(strtotime(date("Y-m-d")) > strtotime($Itinerary->overtime)){                   //对两个时间差进行差运算
            $Itinerary['type'] = '已过期';
        }else{
           $Itinerary['type'] = '未过期';
        }
       
        if($Validate){
            return $this->succ_ess(200, '行程详情', 'type', 1, $Itinerary);
        }
    }
    
    /**
     * @description: 二维码绑定行程和旅客
     * @return {*}
     * @author: 逅来
     */    
    public function bind(){
        $data = $this->request->param('');
        $Validate = new Validate();
        $Validate->bind($data);
       $BindModel =  BindModel::where('itinerary_id',$data['id'])->where('user_id',$data['user_id'])->find();
       if($BindModel){
            return $this->succ_ess(200, '请不要重复绑定', 'type', 0, '');
       }else{
        //   行程设置为以启动
            $ItineraryModel = ItineraryModel::get($data['id']);
            $ItineraryModel->status = 1;
            $ItineraryModel->save();
            
            $Bind = new BindModel();
            $Bind->user_id = $data['user_id'];
            $Bind->itinerary_id = $data['id'];
            $res=  $Bind->save();
            if($res){
                return $this->succ_ess(200, '绑定成功', 'type', 1, '');
            }   
       }
        
    }
    /**
     * @description: 二维码绑定行程和旅客
     * @return {*}
     * @author: 逅来
     */    
    public function del(){
        $data = $this->request->param('');
        $Validate = new Validate();
        $Validate->del($data);
        $Bind = BindModel::where('itinerary_id',$data['id'])->find();
        if($Bind){
            return $this->succ_ess(200, '删除失败，已有旅客绑定行程', 'type', 0, '');
        }else{
            $Itinerary = ItineraryModel::get($data['id']);
            $res = $Itinerary->delete();
            if($res){
                return $this->succ_ess(200, '删除成功', 'type', 1, '');  
            }
        }
    }
    
    

}
